#include "command.hpp"
#include "Log.hpp"

int main()
{
    key_t k = ftok(PATH_NAME,PROJ_ID);
    if(k < 0)
    {
        Log("create key failed" ,Error) << "Client key:" << k << endl; 
    }

    Log("create key done",Debug) << "client key:" << k << endl ; 

    //获取共享内存
    int shmid = shmget(k, SHM_SIZE , 0 );

    if(shmid < 0)
    {
        Log("create shm falied",Error) << "client key:" << k << endl ;
        exit(2);
    }

    Log("create shm success",Error) << "client key:" << k << endl;

    sleep(10);

    char* shmaddr = (char*)shmat(shmid,nullptr,0);
    if(shmaddr == nullptr)
    {
        Log("attach shm failed",Error) << "client key:" << k << endl ;
        exit(3);
    }
    Log("attach shm success",Error) << "client key:" << k << endl ;
    sleep(10);
    
    //使用

    //去关联
    int n = shmdt(shmaddr); 
    //shmaddr参数是指向共享内存段的指针，用于指定要分离的共享内存段。shmdt()函数返回0表示成功，返回-1表示失败。
    assert( n != 1) ; 
    (void) n ;
    Log("detach shm success", Debug) << "client key:"<< shmid << endl;


    //不需要chmctl再次删除
    
    return 0 ;
}
